﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MealBooking.Core;
using MealBooking.Core.Models.Meals;
using ThisIsFirstTest.Models.MealModel;
using ThisIsFirstTest.Filter;
using MealBooking.Core.Models.Accounts;
using MealBooking.Core.Models.Dishes;

namespace ThisIsFirstTest.Controllers.MealController
{
    public class MealController : Controller
    {
        public ActionResult Index()
        {
            var mealCategories = CoreService.MealService.AllMealCategoriesDishTemplatesNotNull();
            return View(mealCategories);
        }

        #region Meal
        public ActionResult OrderNewMeal(string id, string headerMessage = null)
        {
            var mealCategory = CoreService.MealService.MealCategoryDetail(id);
            var dishTemplatesInMealCategory = CoreService.MealService.DishTemplatesInMealCategory(id);
            if (mealCategory == null || dishTemplatesInMealCategory == null)
            {
                return RedirectToAction("Index");
            }
            ViewBag.MealCategory = mealCategory;
            ViewBag.DishTemplatesInMealCategory = dishTemplatesInMealCategory;
            //manager booked today order:
            var manager = CoreService.AccountService.AllManagerInformations().FirstOrDefault();
            var managerOrder = CoreService.OrderService.ManagerTodayOrder(manager.Id);
            if (managerOrder != null && managerOrder.IsBooked)
            {
                ViewBag.IsBooked = true;
            }
            else
            {
                ViewBag.IsBooked = false;
            }
            if (headerMessage != null)
            {
                ViewBag.HeaderMessage = headerMessage;
            }
            return View();
        }
        [HttpPost]
        [Authorize]
        public ActionResult OrderNewMeal(NewMealModel newMealModel, bool orderNow = /*false*/ true)
        {
            #region validate
            //get logon user:
            var accouont = CoreService.AccountService.AccountDetail(new Account { Id = User.Identity.Name });
            if (!accouont.IsActived)
            {
                return RedirectToAction("Index", "Account", new { headerMessage = "Account is not Actived." });
            }
            var customer = CoreService.AccountService.CustomerDetail(User.Identity.Name);
            if (customer.Credit + customer.Balance < 0)
            {
                return RedirectToAction("Index", "Account", new { headerMessage = "Not Enough Balance." });
            }
            //manager booked today order:
            var manager = CoreService.AccountService.AllManagerInformations().FirstOrDefault();
            var managerOrder = CoreService.OrderService.ManagerTodayOrder(manager.Id);
            if (managerOrder != null && managerOrder.IsBooked)
            {
                return RedirectToAction("CustomersTodayOrders", "Order", new { headerMessage = "Already Booked. " });
            }
            #endregion
            var newMeal = Convert(newMealModel);
            var mealWithSum = CoreService.MealService.OrderNewMeal(newMeal);
            if (mealWithSum == null)//验证失败:
            {
                return RedirectToAction("OrderNewMeal", new { id = newMeal.Category.Id, headerMessage = "Not Validated." });
            }
            //add to User Order:
            var order = CoreService.OrderService.AddMealToOrder(accouont.Id, mealWithSum);
            if (orderNow)//TODO: 应该为false
            {
                CoreService.OrderService.OrderUserTodayOrder(accouont.Id);
                return RedirectToAction("TodayOrder", "Order", new { headerMessage = "Order Success. " });
            }
            return RedirectToAction("TodayOrder", "Order", new { headerMessage = "Success." });
        }
        public ActionResult MealDetail(string id, string headerMessage = null)
        {
            var meal = CoreService.MealService.MealDetail(id.ToLower());
            ViewBag.HeaderTitle = headerMessage + "Meal Detail: ";
            return View(meal);
        }
        private Meal Convert(NewMealModel newMeal)
        {
            return new Meal
            {
                Category = new MealCategory { Id = newMeal.CategoryId },
                Dishes = newMeal.DishIds.Select(i => new Dish { Id = i }).ToList(),
            };
        }
        #endregion

        #region MealCategory
        public ActionResult AllMealCategoriesPartial()
        {
            var mealCats = CoreService.MealService.AllMealCategories();
            return PartialView(mealCats);
        }
        //public ActionResult AllMealCategoriesJSON()
        //{
        //    var mealCats = CoreService.MealService.AllMealCategories();
        //    var tempMealCats = mealCats.Select(c => new { Id = c.Id, Name = c.Name });
        //    return Json(tempMealCats);
        //}
        //public ActionResult DishCategoriesJSON(string mealCategoryId)
        //{
        //    var dishCats = CoreService.MealService.DishCategoriesInMealCategory(mealCategoryId);
        //    var dishCatsJSON = dishCats.Select(c => new { Id = c.Id, Name = c.Name });
        //    return Json(dishCatsJSON);
        //}
        #endregion
    }
}
